//
//  JHXiangQingViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-27.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHXiangQingViewController.h"
#import "JHXiangQingTableViewCell.h"
#import "JHxiangqing.h"
#import "NetwordHandler.h"
#import "JHDataBaseManager.h"
#import "JHAblumViewController.h"

@interface JHXiangQingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UISegmentedControl *hotUserSegmen;
@property(nonatomic,retain)UITableView *t;
@property(nonatomic,retain)NSString *leixing;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)JHDataBaseManager *dbManager;
@end

@implementation JHXiangQingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    self.title = self.name2;
    self.arr = [NSMutableArray array];
    self.dbManager = [JHDataBaseManager shareInstance];
    [_dbManager openDB];
    self.nextCursor = 1;
                self.leixing = @"hot";
    [self createHotUserSegmented];
        [self craetTableView];
    [self addHeader];
    [self addFooter];
//    [self getData];
    
}
- (void)createHotUserSegmented
{
    /**
     :returns: 创建分段控制器
     */
    self.hotUserSegmen = [[UISegmentedControl alloc] initWithItems:@[@"最火",@"最进更新",@"经典"]];
    _hotUserSegmen.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 19);

    _hotUserSegmen.selectedSegmentIndex = 0;
    [_hotUserSegmen addTarget:self action:@selector(segmenAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_hotUserSegmen];
    [_hotUserSegmen release];
}
- (void)segmenAction:(UISegmentedControl *)seg
{
    
    //  根据分段标签坐标判断方法
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"最火");
        self.leixing = @"hot";
        [self addHeader];
        [self addFooter];
    }else
        if (seg.selectedSegmentIndex == 1) {
            NSLog(@"最新");
            self.leixing = @"recent";
            [self addHeader];
            [self addFooter];
        }else
            if (seg.selectedSegmentIndex == 2) {
                NSLog(@"经典");
                self.leixing = @"classic";
                [self addHeader];
                [self addFooter];
            }
}
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHXiangQingViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.t addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.t headerBeginRefreshing];
    
}
- (void)addFooter
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHXiangQingViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.t addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonlist:(vc.nextCursor++) + 1];//请求更多数据
        
    }];
}

- (void)GetPersonList:(NSInteger)nextCursor
//- (void)getData
{

    NSString *aa = self.leixing;
    NSLog(@"SSSSSSSSSS%@",aa);
    NSString *t = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=%@&device=android&page=%d&per_page=20&status=0&tag_name=%@",self.title, aa,nextCursor,self.name];
    NSLog(@"%@",t);
    [NetwordHandler requestWithURL:t completion:^(id result) {
        NSLog(@"%@",result);
        NSDictionary *d = result;
        [self.arr removeAllObjects];
        NSMutableArray *a = [d objectForKey:@"list"];
        for (NSDictionary *d3 in a) {
            JHxiangqing *dddd = [[JHxiangqing alloc] initWithDic:d3];
            [_arr addObject:dddd];
        }
        [self.t reloadData];
        [self.t headerEndRefreshing];
        [self.t footerEndRefreshing];
    }];
    
}
- (void)GetPersonlist:(NSInteger)nextCursor
//- (void)getData
{

    NSString *aa = self.leixing;
    NSLog(@"SSSSSSSSSS%@",aa);
    NSString *t = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=%@&device=android&page=%d&per_page=20&status=0&tag_name=%@",self.title, aa,nextCursor,self.name];
    NSLog(@"%@",t);
    [NetwordHandler requestWithURL:t completion:^(id result) {
        NSLog(@"%@",result);
        NSDictionary *d = result;
        
        NSMutableArray *a = [d objectForKey:@"list"];
        for (NSDictionary *d3 in a) {
            JHxiangqing *dddd = [[JHxiangqing alloc] initWithDic:d3];
            [_arr addObject:dddd];
        }
        [self.t reloadData];

        [self.t footerEndRefreshing];
    }];
    
    
    
}


- (void)craetTableView
{
    //  创建tableView
    self.t = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 20 + 20, self.view.frame.size.width, self.view.frame.size.height / 20 * 18 - 85) style:UITableViewStylePlain];
    _t.delegate = self;
    _t.dataSource = self;
       [_t registerClass:[JHXiangQingTableViewCell class] forCellReuseIdentifier:@"s"];
    [self.view addSubview:_t];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 重用池
    JHXiangQingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"s"];
    cell.xiangQing = _arr[indexPath.row];
    
//    [cell.aboutButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    NSArray *arr = [self.dbManager selectInfo];
//    [cell.aboutButton setTitle:@"收藏" forState:UIControlStateNormal];
//    for (JHAudio *audio in arr) {
//        if ([[audio.albumId description] isEqualToString:[cell.xiangQing.xiangId description]]) {
//            [cell.aboutButton setTitle:@"已收藏" forState:UIControlStateNormal];
//            [cell.aboutButton removeTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//            [cell.aboutButton addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
//        }
//    }
//    cell.aboutButton.tag = indexPath.section + 1000;
    
    return cell;
    
}

- (void)buttonAction:(UIButton *)button
{
    NSLog(@"点击的第%ld个按钮", button.tag - 1000);
    JHxiangqing *about = _arr[button.tag - 1000];
    
    [self.dbManager createTable];
    JHAudio *audio = [[JHAudio alloc] init];
    audio.albumId = about.xiangId;
    audio.title = about.title;
    [self.dbManager insertInfoWithlanou:audio];
    [button setTitle:@"已收藏" forState:UIControlStateNormal];
    [self.t reloadData];
}

- (void)buttonAction1:(UIButton *)button
{
    [button setTitle:@"收藏" forState:UIControlStateNormal];
    JHxiangqing *about = _arr[button.tag - 1000];
    [self.dbManager deleteInfoWithNum:[[about.xiangId description] integerValue]];
    [self.t reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  行数
    return _arr.count;
}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return _arr.count;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 点击方法
    JHAblumViewController *e = [[JHAblumViewController alloc] init];
    JHxiangqing *xiangqing = self.arr[indexPath.row];
    e.albumId = [xiangqing xiangId];
    [self.navigationController pushViewController:e animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
